let express = require("express")
let app = express()
var port = ('3000');
app.set('port', port);


//解决跨域
app.use((req, res, next) => {
  // 设置是否运行客户端设置 withCredentials
  // 即在不同域名下发出的请求也可以携带 cookie
  res.header("Access-Control-Allow-Credentials",true)
  // 第二个参数表示允许跨域的域名，* 代表所有域名  
  res.header('Access-Control-Allow-Origin', 'http://localhost')//为什么设置了特定域名还不起效？？
  res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, OPTIONS') // 允许的 http 请求的方法
  // 允许前台获得的除 Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma 这几张基本响应头之外的响应头
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
  if (req.method == 'OPTIONS') {
      res.sendStatus(200)
  } else {
      next()
  }
})

 

// // 1.注册解析 表单数据的 body-parser
// const bodyParser = require('body-parser')
// // 处理post请求的中间件
// app.use(bodyParser.urlencoded({extended:false}))
// 处理json格式的参数的中间件
app.use(express.json())

// 2.导入路由模块,app使用其模块
//2.1登录
let login_router = require("./weteach_routes/login")
app.use("/login",login_router)
//2.2首页数据
let home_router = require("./weteach_routes/home")
app.use("/home",home_router)
//2.3info center
let info_center_router = require("./weteach_routes/info_center")
app.use("/info_center",info_center_router)
//2.4test_library_router
let test_library_router = require("./weteach_routes/test_library")
app.use("/test_library",test_library_router)
//2.5school_router
let school_router = require("./weteach_routes/school")
app.use("/school",school_router)
let latest_releases_router = require("./weteach_routes/latest_release")
app.use("/latest_releases",latest_releases_router)








// // 导入socket.js的封装方法
// var socket = require("./routes/socketio")
// socket.getSocket(server)

var http = require('http');
// // 套接字模块
//创建HTTP server
var server = http.createServer(app);
//此处变成http listen
server.listen(port);
// app.listen(3000,()=>{console.log("服务器启动……")})
// 导出
module.exports = app;


